﻿using System;
using System.Threading;
using System.Windows.Forms;
using ConfExpert.Forms;
using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevExpress.XtraSplashScreen;
using Microsoft.VisualBasic.ApplicationServices;

namespace Tailord
{
    public class SingleInstanceController : WindowsFormsApplicationBase
    {
        public SingleInstanceController()
        {
            IsSingleInstance = true;

            StartupNextInstance += this_StartupNextInstance;
        }

        public void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
        {
            e.BringToForeground = true;
        }

        protected override void OnCreateMainForm()
        {
            using (Login login = new Login())
                if (login.ShowDialog() == DialogResult.OK)
                {
                    AppSession.LaunchingTime = DateTime.Now;

                    //SplashScreenManager.ShowForm(typeof(Splash));
                    AppSession.MainForm = new MainForm();
                    //AppSession.MainForm.ShowInTaskbar = false;
                    AppSession.MainForm.WindowState = FormWindowState.Minimized;

                    MainForm = AppSession.MainForm;
                }
                else
                {
                    Form dummy = new Form();
                    MainForm = dummy;
                    dummy.Close();
                }
        }
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Microsoft.Win32.SystemEvents.UserPreferenceChanged += delegate { };

            InitLookAndFeel();

            Application.ThreadException += new ThreadExceptionEventHandler(new ThreadExceptionHandler().ApplicationThreadException);

            // Add the event handler for handling non-UI thread exceptions to the event. 
            AppDomain.CurrentDomain.UnhandledException += new System.UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            // Set the unhandled exception mode to force all Windows Forms errors to go through our handler.
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
                AppSession.Version = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();

        
           
            SingleInstanceController controller = new SingleInstanceController();
           
            try
            {
                controller.Run(new string[] { });
            }
            catch(Exception ex)
            { 
                
            }

        }

        private static void InitLookAndFeel()
        {
            Application.EnableVisualStyles();
            //Enable form skins here:
            DevExpress.Skins.SkinManager.EnableFormSkins(); // IfNotVista();

            //Register the skins with the designer
            DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.OfficeSkins).Assembly);
        

            Application.SetCompatibleTextRenderingDefault(false);
         
        }

        static void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
        {
            XtraMessageBox.Show(e.ToString(), "Exception");
        }
    }

    public class ThreadExceptionHandler
    {
        public void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
        {
            LogManager.LogToDB(e.Exception.ToString());

            //if (!(e.Exception.ToString().Contains("0x80131904") &&
            //    e.Exception.ToString().Contains("RepositoryManager.RefreshData")))
            //{
            //    XtraMessageBox.Show(e.Exception.ToString(), "Exception");
           
            //}
        }
    }
}
